import { createRouter, createWebHashHistory, type RouteRecordRaw } from 'vue-router' 
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import Layout from "../layout/index.vue"
import Addinventory from "../views/warehouseManagement/inventory/addinventory.vue"
import InventoryRecord from "../views/warehouseManagement/inventory/inventoryRecord.vue"


const routes:RouteRecordRaw[] =[
  {
    path: '/login',
    component: () => import('../views/login/index.vue'),
    meta: { transition: 'slide', requiresAuth: false }
  },
  {
    path: '/',
    component: Layout,  // 关键：使用Layout布局
    meta: { transition: 'slide', requiresAuth: true },
    children: [
      {
        path: '',  // 访问 / 时渲染首页组件
        component: () => import('../views/home/index.vue')
      }
    ]
  },

  // 
  {
    path: "/public",
    component: Layout,
    meta: { transition: 'slide' },
    children: [
      {
        path: 'orderToday', // 访问路径: /system/initialAccount
        component: () => import('../views/public/orderToday/index.vue')
      },
      {
        path: "report",
        component: () => import('../views/public/report/index.vue')
      },
      {
        path: "toreReconciliation",
        component: () => import('../views/public/toreReconciliation/index.vue')
      },
      {
        path: "salaryList",
        component: () => import('../views/public/salaryList/index.vue')
      },
      {
        path: "addProduct",
        component: () => import("../views/public/addProduct/index.vue")
      }
    ]
  },
  {
    path: '/system',
    // component: () => import('../layout/SystemLayout.vue'), // 假设存在系统布局组件
    component: Layout,
    meta: { transition: 'slide' },
    children: [
      {
        path: 'initialAccount', // 访问路径: /system/initialAccount
        component: () => import('../views/system/initialAccount/index.vue')
      },
      {
        path: 'employeeManagement', // 访问路径: /system/employeeManagement
        component: () => import('../views/system/employeeManagement/index.vue'),

      },
      {
        path: 'employeeManagement/clockIn', // 访问路径: /system/employeeManagement/clockIn
        component: () => import('../views/system/employeeManagement/other/clockIn.vue')
      },
      {
        path: 'employeeManagement/workreport', // 访问路径: /system/employeeManagement/workreport
        component: () => import('../views/system/employeeManagement/other/workReport.vue')
      },
      {
        path: 'employeeManagement/details', // 访问路径: /system/employeeManagement/details
        component: () => import('../views/system/employeeManagement/other/details.vue')
      },
      {
        path: 'productList', // 访问路径: /system/productList
        component: () => import('../views/system/productList/index.vue')
      },
      {
        path: "employeeSalary",
        component: () => import('../views/system/employeeSalary/index.vue')
      }
    ]
  },
  {
    path: '/beautySalonManagement',
    component: Layout,
    meta: { transition: 'slide' },
    children: [
      {
        path: "myBeautySalon",  //访问路径: /beautySalonManagement/myBeautySalon
        component: () => import('../views/beautySalonManagement/myBeautySalon/index.vue'),
      },
      {
        path: "newBeautySalon",
        component: () => import('../views/beautySalonManagement/newBeautySalon/index.vue'),
      },
      {
        path: "beautySalonDetails",
        component: () => import('../views/beautySalonManagement/beautySalonDetails/index.vue'),
      }
    ]
  },
  {
    path: "/checkout",
    component: Layout,
    meta: { transition: 'slide' },
    children: [
      {
        path: "paymentReceipt",  //访问路径: /beautySalonManagement/myBeautySalon
        component: () => import('../views/checkout/paymentReceipt/index.vue'),
      },
      {
        path: "salesSlip",
        component: () => import('../views/checkout/salesSlip/index.vue'),
      },
      {
        path: "exchangeOrder",
        component: () => import('../views/checkout/exchangeOrder/index.vue'),
      },
      {
        path: "RWA",
        component: () => import('../views/checkout/RWA/index.vue'),
      },
      {
        path: "capitalAccount",
        component: () => import('../views/checkout/capitalAccount/index.vue'),
      },
      {
        path: "policyAccount",
        component: () => import('../views/checkout/policyAccount/index.vue'),
      },

    ]
  },
  {
    path: "/employeeManagement",
    component: Layout,
    meta: { transition: 'slide' },
    children: [
      {
        path: "managementPerspective",
        component: () => import('../views/employeeManagement/managementPerspective/index.vue'),
      },
      {
        path: "operatorManagement",
        component: () => import('../views/employeeManagement/operatorManagement/index.vue'),
      },
      {
        path: "attendance",
        component: () => import('../views/employeeManagement/attendance/index.vue'),
      },
      {
        path: "performanceAttendance",
        component: () => import('../views/employeeManagement/performanceAttendance/index.vue'),
      }
    ]
  },
  {
    path: "/factory",
    meta: { transition: 'slide' },
    component: Layout,
    children: [
      {
        path: "myFactory",
        component: () => import('../views/factory/myFactory/index.vue'),
      },
      {
        path: "factoryPurchaseOrder",
        component: () => import('../views/factory/factoryPurchaseOrder/index.vue'),
      }
    ]
  },
  {
    path: "/brand",
    meta: { transition: 'slide' },
    component: Layout,
    children: [
      {
        path: "productCategory",
        component: () => import('../views/brand/productCategory/index.vue'),
      },
      {
        path: "brandManagement",
        component: () => import('../views/brand/brandManagement/index.vue'),
      }
    ]
  },
  {
    path: "/warehouseManagement",
    meta: { transition: 'slide' },
    component: Layout,
    children: [
      {
        path: "repositoryList",
        component: () => import('../views/warehouseManagement/repositoryList/index.vue'),
      },
      {
        path: "warehouseAllocation",
        component: () => import('../views/warehouseManagement/warehouseAllocation/index.vue'),
      },
      {
        path: "inventoryAddinventory",
        component: Addinventory,
      },
      {
        path: "inventoryRecord",
        component: InventoryRecord,
      },
      {
        path: "inventoryDamageReport",
        component: () => import('../views/warehouseManagement/inventory/damageReport.vue'),
      },
      {
        path: "inventoryReportOverflowOrder",
        component: () => import('../views/warehouseManagement/inventory/reportOverflowOrder.vue'),
      },
      {
        path: "inventoryChangeWarehouseReceipt",
        component: () => import('../views/warehouseManagement/inventoryChange/warehouseReceipt.vue'),
      },
      {
        path: "warehouseChange",
        component: () => import('../views/warehouseManagement/warehouseChange/index.vue'),
      },
      {
        path: "inventoryAlert",
        component: () => import('../views/warehouseManagement/inventoryAlert/index.vue'),
      },
      {
        path: "expirationAlert",
        component: () => import('../views/warehouseManagement/inventoryAlert/expirationAlert.vue'),
      }
    ]
  },
  {
    path: "/dataStatistics",
    meta: { transition: 'slide' },
    component: Layout,
    children: [
      {
        path: "productsales",
        component: () => import('../views/dataStatistics/productSales/index.vue'),
      },
      {
        path: "slowSeller",
        component: () => import('../views/dataStatistics/slowSeller/index.vue'),
      },
      {
        path: "paymentAmount",
        component: () => import('../views/dataStatistics/paymentAmount/index.vue'),
      },
      {
        path: "brandPerformance",
        component: () => import('../views/dataStatistics/brandPerformance/index.vue'),
      },
      {
        path: "brandShipment",
        component: () => import('../views/dataStatistics/brandShipment/index.vue'),
      },
      {
        path: "brandPaymentCollection",
        component: () => import('../views/dataStatistics/brandPaymentCollection/index.vue'),
      },
      {
        path: "storeData",
        component: () => import('../views/dataStatistics/storeData/index.vue'),
      },
      {
        path: "numberOfOnlineRegistrations",
        component: () => import('../views/dataStatistics/numberOfOnlineRegistrations/index.vue'),
      },
      {
        path: "brandDistribution",
        component: () => import('../views/dataStatistics/brandDistribution/index.vue'),
      },
      {
        path: "storeShipment",
        component: () => import('../views/dataStatistics/storeShipment/index.vue'),
      },
      {
        path: "shippingRanking",
        component: () => import('../views/dataStatistics/shippingRanking/index.vue'),
      }
    ]
  },
  {
    path: '/material',
    meta: { transition: 'slide' },
    component: Layout,
    children: [
      {
        path: 'materialManagement',
        component: () => import('../views/material/materialManagement/index.vue')
      }
    ]
  },
  {
    path: "/liveVideoStreaming",
    meta: { transition: 'slide' },
    component: Layout,
    children: [
      {
        path: "liveManagement",
        component: () => import('../views/liveVideoStreaming/liveManagement/index.vue'),
      }
    ]
  },
  {
    path: '/systemMessage',
    meta: { transition: 'slide' },
    component: Layout,
    children: [
      {
        path: "notification",
        component: () => import('../views/systemMessage/notification/index.vue'),
      },
      {
        path: "operationLog",
        component: () => import('../views/systemMessage/operationLog/index.vue'),
      }
    ]
  },
  {
    path: "/miniProgramManagement",
    meta: { transition: 'slide' },
    component: Layout,
    children: [
      {
        path: "miniProgramBackend",
        component: () => import('../views/miniProgramManagement/miniProgramBackend/index.vue'),
      }
    ]
  },
  {
    path: "/drafts",
    meta: { transition: 'slide' },

    component: () => import('../views/user/drafts/index.vue'),
  },
  {
    path: "/verify",
    meta: { transition: 'slide' },
    component: () => import('../views/user/verify/index.vue'),
  },
  {
    path: "/version",
    meta: { transition: 'slide' },
    component: () => import('../views/user/version/index.vue'),
  },
  {
    path: '/:pathMatch(.*)*',
    component: () => import('../views/404.vue') // 替换为你的404页面路径
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

NProgress.configure({
  showSpinner: false, 
  easing: 'ease-in-out', 
  speed: 300 
})

// 路由跳转前显示进度条
router.beforeEach((to, from, next) => {
 
  if (to.path !== from.path) {
    NProgress.start()
  }
  next()
})


router.beforeEach((to, from, next) => {
  if (to.path !== from.path) {
    NProgress.start()
  }

  // 登录校验：需要登录的页面未登录则跳转到登录页
  const isLogin = localStorage.getItem('token')
  if (to.meta.requiresAuth && !isLogin && to.path !== '/login') {
    next('/login')
  } else {
    next()
  }
})

// 路由跳转完成后隐藏进度条
router.afterEach(() => {
  NProgress.done()
})

export default router